package com.warning_speed_android.socket;

import java.net.InetSocketAddress;
import java.net.SocketAddress;

import org.apache.mina.transport.socket.nio.NioSocketConnector;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

public class CustomClient implements
ClientHandler.Callback {

	private Clientcommunicate client;
	private ClientHandler handler;
	private NioSocketConnector connector;
//	private static final String ADDRESS = "10.0.2.2:8600";
	private static final String ADDRESS = "192.168.1.13:9002";
//	private static final String ADDRESS = "192.168.1.28:8600";
	private String sessionId = null;
	
	public static CustomClient instance = new CustomClient();
	
	public boolean connect(String addressText) {
		connector = new NioSocketConnector();
		SocketAddress address = parseSocketAddress(addressText);
		handler = new ClientHandler(this);
		client = new Clientcommunicate(handler);
		if (client.connect(connector, address, false)) {
			return true;
		}
		return false;
	}

	private SocketAddress parseSocketAddress(String s) {
		s = s.trim();
		int colonIndex = s.indexOf(":");
		if (colonIndex > 0) {
			String host = s.substring(0, colonIndex);
			int port = parsePort(s.substring(colonIndex + 1));
			return new InetSocketAddress(host, port);
		} else {
			int port = parsePort(s.substring(colonIndex + 1));
			return new InetSocketAddress(port);
		}
	}

	private int parsePort(String s) {
		try {
			return Integer.parseInt(s);
		} catch (NumberFormatException nfe) {
			throw new IllegalArgumentException("Illegal port number: " + s);
		}
	}

	@Override
	public void connected() {
		// TODO Auto-generated method stub

	}

	@Override
	public void loggedIn() {
		// TODO Auto-generated method stub

	}

	@Override
	public void loggedOut() {
		// TODO Auto-generated method stub

	}

	@Override
	public void disconnected() {
		// TODO Auto-generated method stub

	}

	@Override
	public void messageReceived(String message) {
		try {
			// TODO Auto-generated method stub
			java.lang.System.out.println(message);
			JSONObject object = new JSONObject(message);
			if(object.getInt("code") >= 0){
				//ok
				JSONObject data = object.getJSONObject("data");
				String cmd = data.getString("cmd");
				switch (cmd) {
				case "loginSocket":
					sessionId = data.getString("sessionId");
					String fbId = data.getJSONObject("user").getString("fbId");
					client.setFbbId(fbId);
					client.setSessionId(sessionId);
					java.lang.System.out.println("sessionId: " + sessionId);
					break;
				case "location":
					break;
				}
			}else{
				Log.e(CustomClient.class.getSimpleName(), "data error");
			}
			
		} catch (Exception e) {
			java.lang.System.out.print(e.getMessage());
		}

	}

	@Override
	public void messageSend(String message) {
		// TODO Auto-generated method stub

	}

	@Override
	public void error(String message) {
		// TODO Auto-generated method stub

	}
	
	public void login(String fbtoken) throws JSONException{
		client.login(fbtoken);
	}
	
	public void sendLocation(String lat, String lon) throws JSONException{
		client.location(lat, lon);
	}
	
//	public void startSendLocation(String typeVehicle) throws JSONException{
//		client.startSendLocation(typeVehicle);
//	}
//	
//	public void stopSendLocation() throws JSONException{
//		client.stopSendLocation();
//	}

}
